<template>
  <div style="width: 300px; margin: 200px auto">
    <div class="form">
      <p class="heading">天津中德应用技术大学</p>
      <input class="input" v-model="SchoolNumber" placeholder="Username" type="text">
      <input class="input" v-model="password" placeholder="Password" type="text">
      <button class="btn" @click="longin">登入</button>
      <button class="btn">注册</button>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'
import {ref} from "vue";
import {useRouter} from 'vue-router'
import request from "../networkRequest/index.js";

export default {
  name: "myLogin",



  setup(){
    const router = useRouter()
    let SchoolNumber = ref('');
    let password = ref('');

    // 定义一个登入的处理函数
    function longin(){
      if(SchoolNumber.value === '' || password.value === ''){
        return  ElMessage({
          type: 'warning',
          showClose: true,
          message: '请填写账号信息！',
          center: true,
        })
      }
      // 登入请求
      request({
        url: 'user/login',
        method: 'post',
        data: {
          SchoolNumber: this.SchoolNumber,
          password: this.password
        }
      }).then(res => {
        if (res.data.status >= 400){
          return  ElMessage({
            type: 'warning',
            showClose: true,
            message: res.data.message,
            center: true,
          })
        }

        console.log(res.data)

        localStorage.setItem('myToken', res.data.tokenStr)

        router.replace({
          name: 'myContainer'
        })

      })


    }

    return{
      SchoolNumber,
      password,
      longin
    }
  }
}
</script>

<style scoped>
.form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  background-color: white;
  padding: 2.5em;
  border-radius: 25px;
  transition: .4s ease-in-out;
  box-shadow: rgba(0, 0, 0, 0.4) 1px 2px 2px;
}

.form:hover {
  transform: translateX(-0.5em) translateY(-0.5em);
  border: 1px solid #171717;
  box-shadow: 10px 10px 0px #666666;
}

.heading {
  color: black;
  padding-bottom: 2em;
  text-align: center;
  font-weight: bold;
}

.input {
  border-radius: 5px;
  border: 1px solid whitesmoke;
  background-color: whitesmoke;
  outline: none;
  padding: 0.7em;
  transition: .4s ease-in-out;
}

.input:hover {
  box-shadow: 6px 6px 0px #969696,
  -3px -3px 10px #ffffff;
}

.input:focus {
  background: #ffffff;
  box-shadow: inset 2px 5px 10px rgba(0,0,0,0.3);
}

.form .btn {
  margin-top: 1em;
  align-self: center;
  padding: 0.7em;
  padding-left: 1em;
  padding-right: 1em;
  border-radius: 10px;
  border: none;
  color: black;
  transition: .4s ease-in-out;
  box-shadow: rgba(0, 0, 0, 0.4) 1px 1px 1px;
}

.form .btn:hover {
  box-shadow: 6px 6px 0px #969696,
  -3px -3px 10px #ffffff;
  transform: translateX(-0.5em) translateY(-0.5em);
}

.form .btn:active {
  transition: .2s;
  transform: translateX(0em) translateY(0em);
  box-shadow: none;
}
</style>